import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class yonghujiemian extends JFrame//用户界面 
{
    int zhanghao;
    JButton zuyongchangdi;
    //JButton guihuanchangdi;
    JButton fanhuizhuye;
    JButton jiaofei;

    yonghujiemianjianshiqi jianshiqi1;//监视器

    public yonghujiemian(int zhanghao) 
    {
        this.zhanghao=zhanghao;
        chushihua();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void chushihua() 
    {
        setBounds(100, 150, 300, 400);
        setTitle("用户"+String.valueOf(zhanghao));
        
        jianshiqi1 = new yonghujiemianjianshiqi(this);

        setLayout(null);

        setLayout(new FlowLayout());
        zuyongchangdi = new JButton("租用或归还场地");
        //guihuanchangdi = new JButton("归还场地");
        fanhuizhuye = new JButton("退出");
        jiaofei=new JButton("缴费");

        add(zuyongchangdi);
        //add(guihuanchangdi);
        add(fanhuizhuye);
        add(jiaofei);

        zuyongchangdi.addActionListener(jianshiqi1);
        //guihuanchangdi.addActionListener(jianshiqi1);
        fanhuizhuye.addActionListener(jianshiqi1);
        jiaofei.addActionListener(jianshiqi1);
    }
    void fanhuizhuye()
    {
        main.fanhuizhuye();
    }
    void zuyongchangdi()
    {
        zuyongchangdichuangkou zuyongchangdichuangkou1=new zuyongchangdichuangkou(this);
    }
    void zuyongchangdi(int yemian)
    {
        zuyongchangdichuangkou zuyongchangdichuangkou1=new zuyongchangdichuangkou(this,yemian);
    }
    /*void guihuanchangdi()
    {
        guihuanchangdichuangkou guihuanchangdichuangkou1=new guihuanchangdichuangkou();
    }*/
    void jiaofei()
    {
        jiaofeichuangkou jiaofeichuangkou1 =new jiaofeichuangkou(this);
    }
}

class yonghujiemianjianshiqi implements ActionListener//用户界面的监视器 
{
    yonghujiemian yonghujiemian1;
    
    yonghujiemianjianshiqi(yonghujiemian yonghujiemian1)
    {
        this.yonghujiemian1=yonghujiemian1;
    }

    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==yonghujiemian1.fanhuizhuye)
        {
            yonghujiemian1.fanhuizhuye();
        }
        else if(e.getSource()==yonghujiemian1.zuyongchangdi)
        {
            yonghujiemian1.zuyongchangdi();
        }
        else if(e.getSource()==yonghujiemian1.jiaofei)
        {
            yonghujiemian1.jiaofei();
        }
        /*else if(e.getSource()==yonghujiemian1.guihuanchangdi)
        {
            yonghujiemian1.guihuanchangdi();
        }*/
    }
}

class zuyongchangdichuangkou extends JFrame//租用场地窗口
{
    int anniushu=10;
    JButton changdi[];
    JButton shangyiye;
    JButton xiayiye;
    JTextField changdixinxi[];
    zuyongchangdichuangkoujianshiqi zuyongchangdichuangkoujianshiqi1;
    yonghujiemian yonghujiemian1;
    int yemian=0;
    zuyongchangdichuangkou(yonghujiemian yonghujiemian1)
    {
        kaiqicichuangkou(yonghujiemian1,0);
    }
    zuyongchangdichuangkou(yonghujiemian yonghujiemian1,int yemian)
    {
        kaiqicichuangkou(yonghujiemian1,yemian);
    }
    private void kaiqicichuangkou(yonghujiemian yonghujiemian1, int yemian) {
        this.setTitle("租借或归还场地");
        
        this.setVisible(true);
        
        this.setLayout(null);
        this.setBounds(100,150 , 600, 600);
        zuyongchangdichuangkoujianshiqi1=new zuyongchangdichuangkoujianshiqi(this);
        this.yonghujiemian1=yonghujiemian1;
        this.yemian = yemian;

        shuaxinshuju(yemian);
        
    }

    void shuaxinshuju()
    {
        shuaxinshuju(yemian);
    }
    void shuaxinshuju(int yemian)
    {
        
        this.yemian=yemian;
        changdi=new JButton[11];
        changdixinxi=new JTextField[11];
        for(int i=1;i<=10;i++)
        {
            changdi[i]=new JButton(Integer.toString(i+10*yemian));
            this.add(changdi[i]);
            changdi[i].setBounds(50,20+(i-1)*50,100,40);
            changdi[i].addActionListener(zuyongchangdichuangkoujianshiqi1);

            String xinxi="0";
            try {
                xinxi=duqu.duqu(i+10*yemian,"shuju\\chuzuqingkuang.txt");
            } catch (IOException e) {
                xinxi="程序读取异常";
            }
            if(xinxi.equals("0"))
            {
                changdixinxi[i]=new JTextField("可租用");
                changdi[i].setEnabled(true);
            }
            else if(xinxi.equals("-1"))
            {
                changdixinxi[i]=new JTextField("停用");
                changdi[i].setEnabled(false);
            }
            else if(xinxi.equals("没有找到数据"))
            { 
                changdi[i].setEnabled(false);
                changdi[i].setVisible(false);
                anniushu=i-1;
                break;
            }
            else if(xinxi.equals(String.valueOf(yonghujiemian1.zhanghao)))
            {
                changdixinxi[i]=new JTextField("你已租用，点击按钮归还");
                changdi[i].setEnabled(true);
            }
            else 
            {
                changdixinxi[i]=new JTextField("已被租用");
                changdi[i].setEnabled(false);
            }
            this.add(changdixinxi[i]);
            changdixinxi[i].setBounds(200,20+(i-1)*50,300,40);
            changdixinxi[i].setEditable(false);
        }
        shangyiye=new JButton("上一页");
        this.add(shangyiye);
        shangyiye.setBounds(50,520, 80, 30);
        shangyiye.setVisible(true);
        shangyiye.addActionListener(zuyongchangdichuangkoujianshiqi1);
        if(yemian==0)
        {
            shangyiye.setEnabled(false);
        }
        else
        {
            shangyiye.setEnabled(true);
        }
        xiayiye=new JButton("下一页");
        this.add(xiayiye);
        xiayiye.setBounds(300,520, 80, 30);
        xiayiye.setVisible(true);
        xiayiye.addActionListener(zuyongchangdichuangkoujianshiqi1);
        if(anniushu!=10)
        {
            xiayiye.setEnabled(false);
        }
        else 
        {
            xiayiye.setEnabled(true);
        }
        
    }

    void zuyonghuoguihuanchangdi(int zhanghao,int changdi)
    {
        try 
        {
            if(duqu.duqu(changdi,"shuju\\chuzuqingkuang.txt").equals(String.valueOf(yonghujiemian1.zhanghao)))//用户已租用，点击按钮归还
            {
                guihuanchangdi(zhanghao, changdi);
                JOptionPane.showMessageDialog(this, "场地"+changdi+"归还成功");
            }
            else 
            {
                zuyongchangdi(zhanghao, changdi);
                JOptionPane.showMessageDialog(this, "场地"+changdi+"租用成功");
            }
        }
        catch (IOException e) 
        {
            
        }
        int yemian=this.yemian;
        this.yonghujiemian1.zuyongchangdi(yemian);
        this.setVisible(false);
    }

    void zuyongchangdi(int zhanghao,int changdi)
    {
        try {
            xieru.xie(changdi, Integer.toString(zhanghao),"shuju\\chuzuqingkuang.txt");
        } catch (IOException e) {
            return;
        }
        try {
            long shijianchuomiao=Math.floorDiv(System.currentTimeMillis(), 1000);
            xieru.xie(changdi,String.valueOf(shijianchuomiao),"shuju\\zuyongshijian.txt");
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    void guihuanchangdi(int zhanghao,int changdi)
    {
        long zujieshijian=0;
        try {
            zujieshijian = Long.parseLong(duqu.duqu(changdi,"shuju\\zuyongshijian.txt"));
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            xieru.xie(changdi, "0","shuju\\chuzuqingkuang.txt");
        } catch (IOException e) {
            return;
        }
        try {
            long shijianchuomiao=Math.floorDiv(System.currentTimeMillis(), 1000);
            xieru.xie(changdi,"0","shuju\\zuyongshijian.txt");
        } catch (Exception e) {
            // TODO: handle exception
        }
        try 
        {
            long shijianchuomiao=Math.floorDiv(System.currentTimeMillis(), 1000);
            if(zujieshijian!=0)
            {
                xieru.zhuijia(String.valueOf(zhanghao)+" "+String.valueOf(changdi)+" "+String.valueOf((shijianchuomiao-zujieshijian)),"shuju\\weijiaomingdan.txt");
            }
            else 
            {
                xieru.zhuijia(String.valueOf(zhanghao)+" "+String.valueOf(changdi)+" "+String.valueOf((0)),"shuju\\weijiaomingdan.txt");
            }
            
        }
        catch(Exception e)
        {

        }
    }
}
class zuyongchangdichuangkoujianshiqi implements ActionListener//租用场地窗口的监视器
{
    zuyongchangdichuangkou zuyongchangdichuangkou1;
    zuyongchangdichuangkoujianshiqi(zuyongchangdichuangkou zuyongchangdichuangkou1)
    {
        this.zuyongchangdichuangkou1=zuyongchangdichuangkou1;
    }
    public void actionPerformed(ActionEvent e) 
    {
        int changdi=0;
        if(e.getSource()==zuyongchangdichuangkou1.changdi[1])
        {
            changdi=1;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.changdi[2])
        {
            changdi=2;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.changdi[3])
        {
            changdi=3;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.changdi[4])
        {
            changdi=4;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.changdi[5])
        {
            changdi=5;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.changdi[6])
        {
            changdi=6;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.changdi[7])
        {
            changdi=7;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.changdi[8])
        {
            changdi=8;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.changdi[9])
        {
            changdi=9;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.changdi[10])
        {
            changdi=10;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.shangyiye)
        {
            int yemian=zuyongchangdichuangkou1.yemian;
            yemian--;
            zuyongchangdichuangkou1.yonghujiemian1.zuyongchangdi(yemian);
            zuyongchangdichuangkou1.setVisible(false);
            return;
        }
        else if(e.getSource()==zuyongchangdichuangkou1.xiayiye)
        {
            int yemian=zuyongchangdichuangkou1.yemian;
            yemian++;
            zuyongchangdichuangkou1.yonghujiemian1.zuyongchangdi(yemian);
            zuyongchangdichuangkou1.setVisible(false);
            return;
        }
        changdi+=zuyongchangdichuangkou1.yemian*10;
        zuyongchangdichuangkou1.zuyonghuoguihuanchangdi(zuyongchangdichuangkou1.yonghujiemian1.zhanghao, changdi);
        zuyongchangdichuangkou1.shuaxinshuju();
    }
}
class jiaofeichuangkou extends JFrame//缴费窗口
{
    JButton jiaofei;
    JTextField jiaofeixinxi;
    jiaofeichuangkoujianshiqi jiaofeichuangkoujianshiqi1;
    yonghujiemian yonghujiemian1;
    int zhanghao=0;
    int hangshu=0;
    jiaofeichuangkou(yonghujiemian yonghujiemian1)
    {
        kaiqicichuangkou(yonghujiemian1);
    }
    private void kaiqicichuangkou(yonghujiemian yonghujiemian1) {
        this.setTitle("缴费");
        this.setVisible(true);
        this.setLayout(null);
        this.setBounds(100,150 , 600, 600);
        jiaofeichuangkoujianshiqi1=new jiaofeichuangkoujianshiqi(this);
        this.yonghujiemian1=yonghujiemian1;
        zhanghao=yonghujiemian1.zhanghao;
        shuaxinshuju();
        
    }
    void shuaxinshuju()
    {
        jiaofei=new JButton("缴费");
        String shuju="0";
        for(int i=1;;i++)
        {
            try {
                shuju=duqu.duqu(i,"shuju\\weijiaomingdan.txt");
            } catch (IOException e) {

            }
            if(shuju.equals("没有找到数据")|| shuju.equals("读取错误") || shuju.equals("0"))
            {
                shuju="当前没有欠款";
                jiaofei.setEnabled(false);
                break;
            }
            else if(fanhuishuzu.threeint(shuju)[0]==zhanghao)
            {
                hangshu=i;
                int danjia=0;
                try {
                    danjia = Integer.valueOf(duqu.duqu(1,"shuju\\danjia.txt"));
                } catch (NumberFormatException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                int xinxi[]=fanhuishuzu.threeint(shuju);
                shuju="用户"+xinxi[0]+"当前欠款"+(((xinxi[2]+1)/3600+1)*danjia)+"元";
                jiaofei.setEnabled(true);
                break;
            }
            else if(fanhuishuzu.threeint(shuju)[0]!=zhanghao)
            {

            }
            else
            {
                break;
            }
        }
        jiaofei.setVisible(true);
        jiaofeixinxi=new JTextField(shuju);
        jiaofeixinxi.setVisible(true);
        jiaofeixinxi.setEditable(false);
        jiaofei.setBounds(150, 200,200,100);
        jiaofeixinxi.setBounds(50, 50,500,100);
        add(jiaofei);
        add(jiaofeixinxi);
        jiaofei.addActionListener(jiaofeichuangkoujianshiqi1);
    }

    void jiaofei(int zhanghao,int hangshu)
    {
        int danjia=0;
        try {
            danjia = Integer.valueOf(duqu.duqu(1,"shuju\\danjia.txt"));
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String xinxi="0 0 0";
        try {
            xinxi = duqu.duqu(hangshu, "shuju\\weijiaomingdan.txt");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int shuju[]=fanhuishuzu.threeint(xinxi);
        String fanhui=String.valueOf(shuju[0])+" "+String.valueOf(shuju[1])+" "+String.valueOf(((shuju[2]+1)/3600+1)*danjia);
        
        
        try {
            xieru.zhuijia(fanhui,"shuju\\shouru.txt");
        } catch (IOException e) {
            return;
        }
        try {
            shanchu.shanchuzhenghang(hangshu,"shuju\\weijiaomingdan.txt");
        } catch (Exception e) {
            // TODO: handle exception
        }
        JOptionPane.showMessageDialog(null,"缴费成功","注意",JOptionPane.CLOSED_OPTION);
        this.setVisible(false);
    }
}
class jiaofeichuangkoujianshiqi implements ActionListener//租用场地窗口的监视器
{
    jiaofeichuangkou jiaofeichuangkou1;
    jiaofeichuangkoujianshiqi(jiaofeichuangkou jiaofeichuangkou1)
    {
        this.jiaofeichuangkou1=jiaofeichuangkou1;
    }
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==jiaofeichuangkou1.jiaofei)
        {
            jiaofeichuangkou1.jiaofei(jiaofeichuangkou1.zhanghao,jiaofeichuangkou1.hangshu);
        }
    }
}